package com.atguigu.eduservice.controller;
import com.atguigu.com.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 * @author atguigu
 * @since 2022-11-07
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin(origins = "*",maxAge = 3600)
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;
    @GetMapping("findAll")
    public R findAllTeacher(){
        final List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }
    //逻辑删除
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/{id}")
    public R removeTeacher(@ApiParam(name="id",value = "讲师id") @PathVariable String id){
        final boolean b = teacherService.removeById(id);
        if (b){
            return R.ok();
        }else{
            return R.error();
        }
    }
    //简单的分页方法
    //current 当前页
    //limit   每页显示条数
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageTeacher(@PathVariable Long current,
                         @PathVariable Long limit){
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现分页
        final IPage<EduTeacher> page = teacherService.page(pageTeacher, null);
        long total = page.getTotal();//总记录数
        List<EduTeacher> records = page.getRecords();//数据list集合
        return R.ok().data("total",total).data("records",records);

    }
    //带条件查询
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) EduTeacher teacher){
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        final QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if ("".equals(teacher)){
            wrapper.like(StringUtils.isNullOrEmpty(teacher.getName()), "name", teacher.getName())
                    .like(teacher.getLevel().equals("") || teacher.getLevel() == null ? false : true, "level", teacher.getLevel())
                    .ge(teacher.getGmtCreate().equals("") || teacher.getLevel() == null ? false : true, "gmt_Create", teacher.getGmtCreate())
                    .le(teacher.getGmtCreate().equals("") || teacher.getLevel() == null ? false : true, "gmt_Create", teacher.getGmtCreate());
        }
        wrapper.orderByDesc("gmt_create");
        //调用方法实现分页
        teacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords();//数据list集合
        return R.ok().data("total",total).data("records",records);

    }

    //添加讲师功能
    @ApiOperation("添加教师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        } else
            return R.error();
    }
    @ApiOperation("根据ID查询教师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher byId = teacherService.getById(id);
        System.out.println(byId);
        return R.ok().data("teacher", byId);
    }

    @ApiOperation("修改教师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean b = teacherService.updateById(eduTeacher);
        if (b) {
            return R.ok();
        } else
            return R.error();
    }

}